<?php
namespace app\admin\validate;
use think\Validate;
use think\Db;

class MemberRegister extends Validate
{
	
	//验证规则
	protected $rule = [	
		'mobile' => 'require|mobile|checkMobile:',				
		'password' => 'password',		
	];
		
	
	//提示信息
	protected $message = [		
		'mobile.require' => '手机号必须填写',
		'mobile.mobile' => '手机号格式错误',
		
		'password.password' => '密码格式6-18个字母数字符号组合',		
	];
	
	
	//自定义正则验证规则
	protected $regex = [
        'mobile'    => '/^(((1[0-9][0-9]{1})|159|153)+\d{8})$/',
        'password'  => '^([a-zA-Z0-9]|[._\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\']){6,18}$',
    ];
	
	
	//自定义检测手机号是否注册
	protected function checkMobile($value,$rule,$data){
		$where = [['mobile', 'eq', $data['mobile']],['is_deleted','eq',0],['usertype','eq',1]];
        if (!empty($data['id'])) $where[] = ['id ', 'neq', $data['id']];
		$count = Db::name('system_user')->where($where)->count();
		if($count){
			return '手机号已注册';
		}else{
			return true;
		}
	}
	
	
	//验证场景
	protected $scene = [
		//注册
		'reg'   => ['mobile','password']
	];
		
}
